package com.winit.cloudAgent.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.cloudAgent.send.Send;
import com.winit.cloudAgent.service.WerbsocketSendService;
import com.winit.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Created by wanglin on 2018/5/24.
 */
@RestController
@Api(value = "cloudAgent", tags = {"cloudAgent"}, description = "cloudAgent")
@RequestMapping(value = "/api/v1/cloudAgent")
public class CloudAgentController {
    @Autowired
    Send send;
    @Autowired
    WerbsocketSendService werbsocketSendService;
    @Autowired
    RedisService redisService;

    @RequestMapping(value = "/test4", method = RequestMethod.GET)
    public void test1(@ApiParam(value = "云链处理结果返回") @RequestParam(value ="id", required = false) String id) throws IOException {
        JSONObject obj =(JSONObject) redisService.getObj(id);
        if(null==obj){
            return;
        }else{
            werbsocketSendService.send(obj);
            redisService.delObj(id);
        }
    }

    @RequestMapping(value = "/sendCloud", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "发送云链消息")
    @ResponseBody
    public void saveNeed(@RequestBody JSONObject jsonObject){
        send.handleJson(jsonObject);
    }


}
